import { RouteObject } from "react-router-dom";
import routeMap from "@/router/dynamicRoutes";
import { Suspense } from "react";

interface MenuItem {
  key: string;
  children?: MenuItem[];
}

/**
 * 将菜单列表转换为路由表
 */
export default function generateRoutes(items: MenuItem[]) {
  const routes: RouteObject[] = [];
  items.forEach((item) => {
    if (item.children) {
      const childrenRoutes = generateRoutes(item.children);
      routes.push(...childrenRoutes);
    } else {
      routes.push({
        path: item.key,
        element: (
          <Suspense fallback={'loading...'}>
            {routeMap[item.key]}
          </Suspense>
        ),
      });
    }
  });
  return routes;
}
